package com.xiaxiaoge.tutorialmod.entity.custom;

import com.mojang.math.Vector3f;
import com.xiaxiaoge.tutorialmod.entity.ModEntityTypes;
import net.minecraft.client.Minecraft;
import net.minecraft.core.particles.DustParticleOptions;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.syncher.EntityDataAccessor;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.HumanoidArm;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.MoverType;
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.phys.Vec3;
import software.bernie.geckolib3.core.IAnimatable;
import software.bernie.geckolib3.core.PlayState;
import software.bernie.geckolib3.core.builder.AnimationBuilder;
import software.bernie.geckolib3.core.controller.AnimationController;
import software.bernie.geckolib3.core.event.predicate.AnimationEvent;
import software.bernie.geckolib3.core.manager.AnimationData;
import software.bernie.geckolib3.core.manager.AnimationFactory;

import java.util.Collections;


/**
 * @author 夏小哥
 * @version 1.0
 * 实体属性
 */
public class VictoriousSwiftTwoEntity extends LivingEntity implements IAnimatable{

    private static final EntityDataAccessor<Integer> COLOR_DATA = SynchedEntityData.defineId(VictoriousSwiftTwoEntity.class, EntityDataSerializers.INT);


    private AnimationFactory factory = new AnimationFactory(this);

    // 用于生成实体的构造函数
    public VictoriousSwiftTwoEntity(Level level, double x, double y, double z) {
        super(ModEntityTypes.VICTORIOUS_SWIFT_TWO.orElseThrow(()-> new IllegalArgumentException("Entity type not registered!")), level);
        this.setPos(x, y, z);
    }

    public VictoriousSwiftTwoEntity(EntityType<? extends LivingEntity> entityEntityType, Level level) {
        super(entityEntityType, level);
    }

//    @Override
//    public void tick() {
//        super.tick();
//        // 添加飞行逻辑，例如速度和方向
//        this.setDeltaMovement(Vec3.ZERO); // 简单的保持静止，实际需要根据输入控制移动
//    }


    @Override
    public void tick() {
        super.tick();

        if (this.isVehicle() && this.getControllingPassenger() instanceof Player player) {
            // 获取玩家输入
            boolean forward = Minecraft.getInstance().options.keyUp.isDown();
            boolean backward = Minecraft.getInstance().options.keyDown.isDown();
            boolean left = Minecraft.getInstance().options.keyLeft.isDown();
            boolean right = Minecraft.getInstance().options.keyRight.isDown();

            // 设置速度和方向
            double speed = 0.1;
            if (forward) {
                this.setDeltaMovement(this.getDeltaMovement().add(0, 0, speed));
            }
            if (backward) {
                this.setDeltaMovement(this.getDeltaMovement().subtract(0, 0, speed));
            }
            if (left) {
                this.setYRot(this.getYRot() - 2);
            }
            if (right) {
                this.setYRot(this.getYRot() + 2);
            }

            // 更新位置和方向
            this.move(MoverType.SELF, this.getDeltaMovement());
        }
    }

    @Override
    public InteractionResult interact(Player player, InteractionHand hand) {
        if (!this.level.isClientSide && !player.isPassenger()) {
            player.startRiding(this);
            return InteractionResult.SUCCESS;
        }
        return super.interact(player, hand);
    }

    @Override
    public HumanoidArm getMainArm() {
        return null;
    }

//    public InteractionResult interact(Player player, InteractionHand hand) {
//        if (hand == InteractionHand.MAIN_HAND) {
//            this.shootLaser(); // 发射光线
//            return InteractionResult.SUCCESS;
//        }
//        return super.interact(player, hand);
//    }

    // 方法用于发射粒子光线炮
    public void shootLaser() {
        Vec3 lookVec = this.getLookAngle(); // 获取当前朝向
        Vec3 startPos = this.position().add(0, this.getEyeHeight(), 0); // 起点位置

        // 创建一个 DustParticleOptions 对象
        DustParticleOptions dustOptions = new DustParticleOptions(new Vector3f(1.0F, 0.0F, 0.0F), 1.0F);

        for (int i = 0; i < 100; i++) {
            Vec3 particlePos = startPos.add(lookVec.scale(i * 0.1)); // 沿着朝向生成粒子
            level.addParticle(dustOptions, particlePos.x, particlePos.y, particlePos.z, 0.0, 0.0, 0.0);
        }
    }

    @Override
    public Packet<?> getAddEntityPacket() {
        return super.getAddEntityPacket();
    }


    @Override
    public AnimationFactory getFactory() {
        return factory;
    }

    public static AttributeSupplier.Builder createAttributes() {
        return LivingEntity.createLivingAttributes()
                .add(Attributes.MAX_HEALTH, 20.0) // Example attribute
                .add(Attributes.MOVEMENT_SPEED, 0.5) // Example attribute
                .add(Attributes.FOLLOW_RANGE, 35.0) // 定义跟随距离属性
                .add(Attributes.ATTACK_DAMAGE, 5.0);
    }


    @Override
    public Iterable<ItemStack> getArmorSlots() {
        return Collections.emptyList();
    }

    @Override
    public ItemStack getItemBySlot(EquipmentSlot p_21127_) {
        return ItemStack.EMPTY;
    }

    @Override
    public void setItemSlot(EquipmentSlot p_21036_, ItemStack p_21037_) {

    }

    @Override
    public void registerControllers(AnimationData data) {
        data.addAnimationController(new AnimationController(this, "controller",
                0, this::predicate));
    }

    private <E extends IAnimatable> PlayState predicate(AnimationEvent<E> event) {
        if (event.isMoving()) {
            event.getController().setAnimation(new AnimationBuilder().addAnimation("animation.model.openweapon", true));
            return PlayState.CONTINUE;
        }
        event.getController().setAnimation(new AnimationBuilder().addAnimation("animation.model.openweapon", true));
        return PlayState.CONTINUE;
    }
}
